﻿using System;
using static ZquenceStudio.DefaultDspPlugins.Effects.Distortion;

namespace DefaultDspPluginsLib.Depricated
{
    public static class Effect_Distortion
    {
        public static void Process(ref DistortionData data, double[] wav_in_left, double[] wav_in_right, double[] wav_out_left, double[] wav_out_right)
        {
            double a = data.Dist > 0.98 ? 0.98 : data.Dist;
            double k = 2 * a / (1 - a);

            // Reference: http://www.musicdsp.org/archive.php?classid=4
            for (int c = 0; c < data.Channels; c++)
            {
                double[] inputSamples = c == 0 ? wav_in_left : wav_in_right;
                double[] outputSamples = c == 0 ? wav_out_left : wav_out_right;

                for (int i = data.Length - 1; i >= 0; i--)
                {
                    double x = inputSamples[i];
                    outputSamples[i] = ((1 + k) * x / (1 + k * Math.Abs(x))) * data.Output;
                }
            }
        }
    }
}
